package org.yzh.scheduler.model.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import org.yzh.commons.util.DateUtils;
import org.yzh.commons.util.EncryptUtils;

import java.io.*;
import java.time.LocalDateTime;
import java.util.Base64;

public class JobLogDO {

    @Schema(description = "ID")
    private Integer id;
    @Schema(description = "任务ID")
    private Integer jobId;
    @Schema(description = "任务名称(接口名)")
    private String jobName;
    @Schema(description = "任务组(系统名)")
    private String jobGroup;

    @Schema(description = "HttpMethod")
    private String method;
    @Schema(description = "HttpURL")
    private String url;
    @Schema(description = "HttpContentType")
    private String contentType;
    @Schema(description = "HttpParameters")
    private String parameters;

    @Schema(description = "触发时间")
    private LocalDateTime triggerTime;
    @Schema(description = "触发响应码")
    private Integer triggerCode;
    @Schema(description = "触发响应报文")
    private String triggerMsg;
    @Schema(description = "回调时间")
    private LocalDateTime callbackTime;
    @Schema(description = "回调响应码")
    private Integer callbackCode;
    @Schema(description = "回调报文")
    private String callbackMsg;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getJobId() {
        return jobId;
    }

    public void setJobId(Integer jobId) {
        this.jobId = jobId;
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getJobGroup() {
        return jobGroup;
    }

    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }

    public LocalDateTime getTriggerTime() {
        return triggerTime;
    }

    public void setTriggerTime(LocalDateTime triggerTime) {
        this.triggerTime = triggerTime;
    }

    public Integer getTriggerCode() {
        return triggerCode;
    }

    public void setTriggerCode(Integer triggerCode) {
        this.triggerCode = triggerCode;
    }

    public String getTriggerMsg() {
        return triggerMsg;
    }

    public void setTriggerMsg(String triggerMsg) {
        this.triggerMsg = format(triggerMsg);
    }

    public LocalDateTime getCallbackTime() {
        return callbackTime;
    }

    public void setCallbackTime(LocalDateTime callbackTime) {
        this.callbackTime = callbackTime;
    }

    public Integer getCallbackCode() {
        return callbackCode;
    }

    public void setCallbackCode(Integer callbackCode) {
        this.callbackCode = callbackCode;
    }

    public String getCallbackMsg() {
        return callbackMsg;
    }

    public void setCallbackMsg(String callbackMsg) {
        this.callbackMsg = format(callbackMsg);
    }

    public static String format(String message) {
        if (message != null) {
            message = message.replace("  ", " ").replace("  ", " ").replace("\t\t", "\t");
            if (message.length() > 2048)
                message = message.substring(0, 2047);
        }
        return message;
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        JobLogDO other = (JobLogDO) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        return result;
    }


    public static String toKey(JobLogDO jobLog) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(64);
             DataOutputStream dos = new DataOutputStream(bos)) {

            dos.writeUTF(jobLog.jobGroup);
            dos.writeUTF(jobLog.jobName);
            dos.writeInt(jobLog.id);
            dos.writeLong(DateUtils.getMillis(jobLog.triggerTime));

            byte[] bytes = bos.toByteArray();
            bytes = EncryptUtils.encrypt(bytes);
            return Base64.getUrlEncoder().encodeToString(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static JobLogDO fromKey(String str) {
        byte[] bytes = Base64.getUrlDecoder().decode(str);
        bytes = EncryptUtils.decrypt(bytes);
        if (bytes == null)
            return null;

        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
             DataInputStream dis = new DataInputStream(bis)) {

            JobLogDO jobLog = new JobLogDO();
            jobLog.setJobGroup(dis.readUTF());
            jobLog.setJobName(dis.readUTF());
            jobLog.setId(dis.readInt());
            jobLog.setTriggerTime(DateUtils.getDateTime(dis.readLong()));
            return jobLog;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String toString() {
        return "JobLogDO{" +
                "id=" + id +
                ", jobId=" + jobId +
                ", jobName='" + jobName + '\'' +
                ", jobGroup='" + jobGroup + '\'' +
                ", method='" + method + '\'' +
                ", url='" + url + '\'' +
                ", contentType='" + contentType + '\'' +
                ", parameters='" + parameters + '\'' +
                ", triggerTime=" + triggerTime +
                ", triggerCode=" + triggerCode +
                ", triggerMsg='" + triggerMsg + '\'' +
                ", callbackTime=" + callbackTime +
                ", callbackCode=" + callbackCode +
                ", callbackMsg='" + callbackMsg + '\'' +
                '}';
    }
}